package com.qisu.converters;

import java.util.Arrays;

import org.springframework.stereotype.Component;

/**
 *
 * @author alsan
 *
 */
@Component
public final class StrConverter implements FieldConverter<String> {

	@Override
	public String converter(byte[] data, int pos, int len) {
		if(null != data && data.length > 0) {
			byte[] d = Arrays.copyOfRange(data, pos, pos + len);
			StringBuffer buf = new StringBuffer();

			for(byte b : d) {
				if(b >= 0x20 && b < 0x80) {
					buf.append((char)b);
				}
			}
			return buf.toString();
		}
		return null;
	}

	@Override
	public String converter(byte[] data) {
		return converter(data, 0, data.length);
	}

}
